Imports System.Collections.Generic
Imports System.Windows.Forms

Imports Vintasoft.Imaging
Imports Vintasoft.Pdf
Imports Vintasoft.Pdf.Tree

Namespace Pdf
	Public Partial Class OptionalContentSettingsForm
		Inherits Form

		#Region "Fields"

		Private _viewer As ImageViewer

		Private _initialization As Boolean = False

		Private _document As PdfDocument

		Private _configurations As New List(Of PdfOptionalContentConfiguration)()

		#End Region



		#Region "Constructors"

		Public Sub New(document As PdfDocument, viewer As ImageViewer)
			_initialization = True

			InitializeComponent()

			_document = document
			_viewer = viewer

			Dim defaultName As String = String.Format("{0}(Default)", _document.OptionalContentProperties.DefaultConfiguration.Name)
			AddConfiguration(defaultName, _document.OptionalContentProperties.DefaultConfiguration)
			Dim configurations As IList(Of PdfOptionalContentConfiguration) = _document.OptionalContentProperties.Configurations
			If configurations IsNot Nothing Then
				For i As Integer = 0 To configurations.Count - 1
					AddConfiguration(configurations(i).Name, configurations(i))
				Next
			End If
			configurationsComboBox.SelectedIndex = _configurations.IndexOf(_document.OptionalContentConfiguration)

			_initialization = False
		End Sub

		#End Region



		#Region "Methods"

		Private Sub AddConfiguration(name As String, configuration As PdfOptionalContentConfiguration)
			If name = "" Then
				name = "(no name)"
			End If
			configurationsComboBox.Items.Add(name)
			_configurations.Add(configuration)
		End Sub

		Private Sub configurationsComboBox_SelectedIndexChanged(sender As Object, e As EventArgs)
			UpdateCurrentConfiguration()
			_document.OptionalContentConfiguration = _configurations(configurationsComboBox.SelectedIndex)
			LoadGroupsList()
		End Sub

		Private Sub LoadGroupsList()
			_initialization = True
			ocGroupsCheckedListBox.Items.Clear()
			Dim groups As IList(Of PdfOptionalContentGroup) = _document.OptionalContentProperties.OptionalContentGroups
			Dim configuration As PdfOptionalContentConfiguration = _document.OptionalContentConfiguration
			For Each group As PdfOptionalContentGroup In groups
				ocGroupsCheckedListBox.Items.Add(group.Name, configuration.GetGroupVisibility(group))
			Next
			_initialization = False
		End Sub

		Private Sub UpdateCurrentConfiguration()
			If ocGroupsCheckedListBox.Items.Count > 0 Then
				_initialization = True
				Dim groups As IList(Of PdfOptionalContentGroup) = _document.OptionalContentProperties.OptionalContentGroups
				Dim configuration As PdfOptionalContentConfiguration = _document.OptionalContentConfiguration
				For i As Integer = 0 To groups.Count - 1
					configuration.SetGroupVisibility(groups(i), ocGroupsCheckedListBox.GetItemChecked(i))
				Next
				_initialization = False
			End If
		End Sub

		Private Sub buttonOk_Click(sender As Object, e As EventArgs)
			UpdateCurrentConfiguration()
			DialogResult = DialogResult.OK
		End Sub

		#End Region

	End Class
End Namespace
